﻿/*  ------------------------------------------------------------------------
	 NQ Common Services Library
	 
	 Homepage: http://www.awzhome.de/
	 ------------------------------------------------------------------------
	 
	 The contents of this file are subject to the Mozilla Public License
	 Version 1.1 (the "License"); you may not use this file except in
	 compliance with the License. You may obtain a copy of the License at
	 http://www.mozilla.org/MPL/
	 
	 Software distributed under the License is distributed on an "AS IS"
	 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	 License for the specific language governing rights and limitations
	 under the License.
	
	 The Original Code is code of NQ Common Services Library.

	 The Initial Developer of the Original Code is Andreas Weizel.
	 Portions created by the Initial Developer are
	 Copyright (C) 2011 Andreas Weizel. All Rights Reserved.
	 
	 Contributor(s): (none)
	 
	 Alternatively, the contents of this file may be used under the terms
	 of the GNU General Public License Version 2.0 or later (the "GPL"),
	 or the GNU Lesser General Public License Version 2.1 or later (the
	 "LGPL"), in which case the provisions of the GPL or the LGPL are
	 applicable instead of those above. If you wish to allow use of your
	 version of this file only under the terms of the GPL or the LGPL and
	 not to allow others to use your version of this file under the MPL,
	 indicate your decision by deleting the provisions above and replace
	 them with the notice and other provisions required by the GPL or the
	 LGPL. If you do not delete the provisions above, a recipient may use
	 your version of this file under the terms of any of the MPL, the GPL
	 or the LGPL.
	 ------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AWZhome.NQ.CommonServices
{
	/// <summary>
	/// Class containing some extension methods for easier usage of
	/// <see cref="AWZhome.NQ.CommonServices.IConfigManager"/>
	/// interface.
	/// </summary>
	public static class ConfigServiceExtensions
	{
		private const string ComponentsCategory = "components";
		private const string ContainersCategory = "containers";

		/// <summary>
		/// Returns the list of all component configurations.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <returns>Array of components identifiers.</returns>
		public static string[] GetComponentList(this IConfigManager configService)
		{
			return configService.GetGroupList(ComponentsCategory);
		}

		/// <summary>
		/// Returns the list of all container configurations.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <returns>Array of components identifiers.</returns>
		public static string[] GetContainerList(this IConfigManager configService)
		{
			return configService.GetGroupList(ContainersCategory);
		}

		/// <summary>
		/// Returns a component setting.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="componentName">Component identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <returns>A <see cref="AWZhome.NQ.CommonServices.ConfigData">ConfigData</see> instance with the setting data.</returns>
		public static ConfigData GetComponentSetting(this IConfigManager configService, string componentName, string settingName)
		{
			return configService.GetSetting(ComponentsCategory, componentName, settingName);
		}

		/// <summary>
		/// Returns a container setting.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="containerName">Container identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <returns>A <see cref="AWZhome.NQ.CommonServices.ConfigData">ConfigData</see> instance with the setting data.</returns>
		public static ConfigData GetContainerSetting(this IConfigManager configService, string containerName, string settingName)
		{
			return configService.GetSetting(ContainersCategory, containerName, settingName);
		}

		/// <summary>
		/// Returns a list setting of a component.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="componentName">Component identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <returns>A list of <see cref="AWZhome.NQ.CommonServices.ConfigData">ConfigData</see> instances with the setting data.</returns>
		public static IList<ConfigData> GetComponentListSetting(this IConfigManager configService, string componentName, string settingName)
		{
			return configService.GetListSetting(ComponentsCategory, componentName, settingName);
		}

		/// <summary>
		/// Returns a list setting of a container.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="containerName">Container identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <returns>A list of <see cref="AWZhome.NQ.CommonServices.ConfigData">ConfigData</see> instances with the setting data.</returns>
		public static IList<ConfigData> GetContainerListSetting(this IConfigManager configService, string containerName, string settingName)
		{
			return configService.GetListSetting(ContainersCategory, containerName, settingName);
		}

		/// <summary>
		/// Sets the value of a component setting.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="componentName">Component identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <param name="data">A <see cref="AWZhome.NQ.CommonServices.ConfigData">ConfigData</see> instance with the setting data.</param>
		/// <param name="userSetting">Sets if the setting should be written to the user-specific configuration.</param>
		/// <returns><c>True</c>, if the value could be set successfully.</returns>
		public static bool SetComponentSetting(this IConfigManager configService, string componentName, string settingName, ConfigData data, bool userSetting)
		{
			return configService.SetSetting(ComponentsCategory, componentName, settingName, data, userSetting);
		}

		/// <summary>
		/// Sets the value of a container setting.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="containerName">Container identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <param name="data">A <see cref="AWZhome.NQ.CommonServices.ConfigData">ConfigData</see> instance with the setting data.</param>
		/// <param name="userSetting">Sets if the setting should be written to the user-specific configuration.</param>
		/// <returns><c>True</c>, if the value could be set successfully.</returns>
		public static bool SetContainerSetting(this IConfigManager configService, string containerName, string settingName, ConfigData data, bool userSetting)
		{
			return configService.SetSetting(ContainersCategory, containerName, settingName, data, userSetting);
		}

		/// <summary>
		/// Sets a list setting of a component.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="componentName">Component identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <param name="data">A list of <see cref="AWZhome.NQ.CommonServices.ConfigData">ConfigData</see> instances with the setting data.</param>
		/// <param name="userSetting">Sets if the setting should be written to the user-specific configuration.</param>
		/// <returns><c>True</c>, if the value could be set successfully.</returns>
		public static bool SetComponentListSetting(this IConfigManager configService, string componentName, string settingName, IList<ConfigData> data, bool userSetting)
		{
			return configService.SetListSetting(ComponentsCategory, componentName, settingName, data, userSetting);
		}

		/// <summary>
		/// Sets a list setting of a container.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="containerName">Container identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <param name="data">A list of <see cref="AWZhome.NQ.CommonServices.ConfigData">ConfigData</see> instances with the setting data.</param>
		/// <param name="userSetting">Sets if the setting should be written to the user-specific configuration.</param>
		/// <returns><c>True</c>, if the value could be set successfully.</returns>
		public static bool SetContainerListSetting(this IConfigManager configService, string containerName, string settingName, IList<ConfigData> data, bool userSetting)
		{
			return configService.SetListSetting(ContainersCategory, containerName, settingName, data, userSetting);
		}

		/// <summary>
		/// Removes a component setting from configuration.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="componentName">Component identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <param name="userSetting">Sets if the setting should be removed from the user-specific configuration.</param>
		/// <returns><c>True</c>, if the setting could be removed successfully.</returns>
		public static bool RemoveComponentSetting(this IConfigManager configService, string componentName, string settingName, bool userSetting)
		{
			return configService.RemoveSetting(ComponentsCategory, componentName, settingName, userSetting);
		}

		/// <summary>
		/// Removes a container setting from configuration.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="containerName">Container identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <param name="userSetting">Sets if the setting should be removed from the user-specific configuration.</param>
		/// <returns><c>True</c>, if the setting could be removed successfully.</returns>
		public static bool RemoveContainerSetting(this IConfigManager configService, string containerName, string settingName, bool userSetting)
		{
			return configService.RemoveSetting(ContainersCategory, containerName, settingName, userSetting);
		}

		/// <summary>
		/// Removes list setting of a component from configuration.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="componentName">Component identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <param name="userSetting">Sets if the setting should be removed from the user-specific configuration.</param>
		/// <returns><c>True</c>, if the setting could be removed successfully.</returns>
		public static bool RemoveComponentListSetting(this IConfigManager configService, string componentName, string settingName, bool userSetting)
		{
			return configService.RemoveListSetting(ComponentsCategory, componentName, settingName, userSetting);
		}

		/// <summary>
		/// Removes list setting of a container from configuration.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="containerName">Container identifier.</param>
		/// <param name="settingName">Name of the setting.</param>
		/// <param name="userSetting">Sets if the setting should be removed from the user-specific configuration.</param>
		/// <returns><c>True</c>, if the setting could be removed successfully.</returns>
		public static bool RemoveContainerListSetting(this IConfigManager configService, string containerName, string settingName, bool userSetting)
		{
			return configService.RemoveListSetting(ContainersCategory, containerName, settingName, userSetting);
		}

		/// <summary>
		/// Renames a component group keeping all settings it contains.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="oldGroupName">Old name of the group.</param>
		/// <param name="newGroupName">New name of the group</param>
		/// <param name="userSetting">Sets if the setting should be removed from the user-specific configuration.</param>
		/// <returns><c>True</c>, if renaming was successful, <c>false</c> otherwise.</returns>
		public static bool RenameComponentGroup(this IConfigManager configService, string oldGroupName, string newGroupName, bool userSetting)
		{
			return configService.RenameGroup(ComponentsCategory, oldGroupName, newGroupName, userSetting);
		}

		/// <summary>
		/// Renames a container group keeping all settings it contains.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="oldGroupName">Old name of the group.</param>
		/// <param name="newGroupName">New name of the group</param>
		/// <param name="userSetting">Sets if the setting should be removed from the user-specific configuration.</param>
		/// <returns><c>True</c>, if renaming was successful, <c>false</c> otherwise.</returns>
		public static bool RenameContainerGroup(this IConfigManager configService, string oldGroupName, string newGroupName, bool userSetting)
		{
			return configService.RenameGroup(ContainersCategory, oldGroupName, newGroupName, userSetting);
		}

		/// <summary>
		/// Removes a component group with its settings from configuration.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="componentName">Component group to remove.</param>
		/// <param name="userSetting">Sets if the setting should be removed from the user-specific configuration.</param>
		/// <returns><c>True</c> if successful, <c>false</c> otherwise.</returns>
		public static bool RemoveComponentGroup(this IConfigManager configService, string componentName, bool userSetting)
		{
			return configService.RemoveGroup(ComponentsCategory, componentName, userSetting);
		}

		/// <summary>
		/// Removes a container group with its settings from configuration.
		/// </summary>
		/// <param name="configService">Configuration service instance.</param>
		/// <param name="containerName">Container group to remove.</param>
		/// <param name="userSetting">Sets if the setting should be removed from the user-specific configuration.</param>
		/// <returns><c>True</c> if successful, <c>false</c> otherwise.</returns>
		public static bool RemoveContainerGroup(this IConfigManager configService, string containerName, bool userSetting)
		{
			return configService.RemoveGroup(ContainersCategory, containerName, userSetting);
		}
	}
}
